package main

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
)

func Query(c *gin.Context) {
	dsn := "root:root@tcp(localhost:3306)/golang"
	// "账号:密码@tcp(localhost:端口号)/数据库名"
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	sqlStr := "SELECT * FROM student"
	// "SELECT * FROM 表名"
	studentSlice := MySQLQuery(db, sqlStr)
	titleStr := "This is a HelloWorld template"
	c.HTML(http.StatusOK, "index.html", gin.H{
		"student_slice": studentSlice, // 从数据库中查询到的结果
		"title":         titleStr,
	})
}

func Add(c *gin.Context) {
	dsn := "root:root@tcp(localhost:3306)/golang"
	// "账号:密码@tcp(localhost:端口号)/数据库名"
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	Id := c.PostForm("Id")
	Name := c.PostForm("Name")
	// fmt.Println(Id)
	// fmt.Println(Name)

	sqlStr := "INSERT INTO student (id, name) VALUES (" + Id + ",'" + Name + "');"
	fmt.Println(sqlStr)
	res, err2 := db.Exec(sqlStr)
	if err2 != nil {
		log.Fatal(err)
	}
	fmt.Println(res.RowsAffected())
	c.Redirect(http.StatusMovedPermanently, "/index")
}

func DeletePage(c *gin.Context) {
	c.HTML(http.StatusOK, "delete_page.html", 0)
}

func DeleteOneRow(c *gin.Context) {
	dsn := "root:root@tcp(localhost:3306)/golang"
	// "账号:密码@tcp(localhost:端口号)/数据库名"
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	Id := c.PostForm("Id")
	// fmt.Println(Id)
	sqlStr := "DELETE FROM student WHERE id = " + Id + ";"
	fmt.Println(sqlStr)

	// sqlStr := "INSERT INTO student (id, name) VALUES (" + Id + ",'" + Name + "');"
	// fmt.Println(sqlStr)
	// 需要加一个判断Id变量合法性的方法
	res, err2 := db.Exec(sqlStr)
	if err2 != nil {
		log.Fatal(err)
	}
	fmt.Println(res.RowsAffected())
	c.Redirect(http.StatusMovedPermanently, "/index")
}

func DeleteDirect(c *gin.Context) {
	dsn := "root:root@tcp(localhost:3306)/golang"
	// "账号:密码@tcp(localhost:端口号)/数据库名"
	db, err := sql.Open("mysql", dsn)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	Id := c.PostForm("Id")

	fmt.Println(Id)
	// sqlStr := "DELETE FROM student WHERE id = " + Id + ";"
	// fmt.Println(sqlStr)

	// sqlStr := "INSERT INTO student (id, name) VALUES (" + Id + ",'" + Name + "');"
	// fmt.Println(sqlStr)
	// 需要加一个判断Id变量合法性的方法
	// res, err2 := db.Exec(sqlStr)
	// if err2 != nil {
	// 	log.Fatal(err)
	// }
	// fmt.Println(res.RowsAffected())
	c.Redirect(http.StatusMovedPermanently, "/index")
}

func SetupRouter() *gin.Engine {
	router := gin.Default()
	router.LoadHTMLGlob("templates/*")
	router.Static("css", "asset/css")
	router.GET("/index", Query)
	router.GET("/delete_page", DeletePage)
	router.POST("/add", Add)
	router.POST("/delete_direct", DeleteDirect)
	router.POST("/delete_one_row", DeleteOneRow)
	return router
}

type student struct {
	Id   int
	Name string
}

func MySQLQuery(db *sql.DB, sqlStr string) []student {
	rows, err := db.Query(sqlStr)
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	students := make([]student, 0)

	for rows.Next() {
		stu1 := student{}
		if err := rows.Scan(&stu1.Id, &stu1.Name); err != nil {
			log.Fatal(err)
		}
		students = append(students, stu1)
	}
	return students
}

func main() {
	router := SetupRouter()
	router.Run()
}
